package retrofit.boot.hystrix;

import lombok.Data;
import org.apache.commons.collections4.MapUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by wulg on 2019/10/23
 * description: Hystrix定义配置
 */
@Data
@ConfigurationProperties(prefix = "hystrix")
public class HystrixProperties {

    private String defaultFileName = "retrofit-hystrix.properties";

    private Map<String, Properties> command = new HashMap<>();

    private Map<String, Properties> threadpool = new HashMap<>();

    public Properties getHystrixCommand(){
        return newProperties("hystrix.command",this.command);
    }

    public Properties getHystrixThreadpool(){
        return newProperties("hystrix.threadpool",this.threadpool);
    }

    private Properties newProperties(String prefixName,Map<String, Properties> map){
        Properties newProperties = new Properties();
        if(MapUtils.isNotEmpty(map)){
            for(Map.Entry<String, Properties> entry : map.entrySet()){
                Properties oldProperties = entry.getValue();
                for (Object oldKey : oldProperties.keySet()) {
                    String newKey = prefixName+"."+entry.getKey()+"."+oldKey.toString();
                    newProperties.setProperty(newKey,oldProperties.getProperty(oldKey.toString()));
                }
            }
        }
        return newProperties;
    }
}
